package com.senjoeson.baselib_ui.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.support.annotation.IntRange
import android.text.TextPaint
import android.util.AttributeSet
import android.util.Log
import android.view.View

/**
 *
 *@author sunqiao
 *@date 2018/7/30 10:23
 */
class CustomProgressView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : View(context, attrs, defStyleAttr) {
    val TAG = "CustomProgressView"
    private val sAutoScaleX = 1.78f
    private val mRingStrokeWidth = 4 * sAutoScaleX
    private val mTextSize = 36 * sAutoScaleX

    var mContentPaint: Paint
    var mRectF: RectF
    var mProgress: Int = 0
    var mTextPaint: TextPaint
    var times: Int = 0
    val mFillColor = -0X3A9F
    var defaultColor = -0x232324




    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawCircle(canvas)
        drawText(canvas)
    }

    init {
        //拿到一个paint对象
        mContentPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mContentPaint.isAntiAlias = true  //去锯齿
        mContentPaint.style = Paint.Style.STROKE //加粗
        mContentPaint.strokeWidth = mRingStrokeWidth //加粗的大小

        mTextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
        mTextPaint.textAlign = Paint.Align.CENTER
        mTextPaint.textSize = mTextSize
        mTextPaint.color = mFillColor

        mRectF = RectF()
    }

    /**
     * 绘制文字
     */
    fun drawText(canvas: Canvas) {
        val x = width / 2f
        val y = height / 2f - (mTextPaint.ascent() + mTextPaint.baselineShift) / 2
        val content = (mProgress*animatedFraction).toInt().toString() + "%"
        Log.d(TAG, content)
        canvas.drawText(content, x, y, mTextPaint)
    }

    /**
     * 绘制圆圈
     */
    fun drawCircle(canvas: Canvas) {

        mContentPaint.color = defaultColor
        val left = mRingStrokeWidth / 2
        val top = mRingStrokeWidth / 2
        val right = width - mRingStrokeWidth / 2
        val bottom = height - mRingStrokeWidth / 2
        mRectF.set(left, top, right, bottom)
        Log.d(TAG, "左边:" + left + ",顶部:" + top + ",右边" + right + ",底部" + bottom)

        //画一条灰色的圈
        canvas.drawArc(mRectF, 0f, 360f, false, mContentPaint)
        //填充黄色
        mContentPaint.color = mFillColor
        //画一个黄色的圈
        canvas.drawArc(mRectF, -90f, 360f * mProgress  / 100*animatedFraction, false, mContentPaint)

        initAnimation()

    }

    var animatedFraction = 0f
    var animator: ValueAnimator?=null

    private fun initAnimation() {
        animator= ValueAnimator.ofFloat(0f,1f)
        animator?.setDuration(2000)
        animator?.addUpdateListener(animatorUpdateListener)

    }

    /**
     * 设置进度条时,刷新动画进度
     */
    fun setProgress(@IntRange(from = 0, to = 100) progress: Int) {
        mProgress = progress
        animator?.start()
        invalidate()
    }


    val animatorUpdateListener = ValueAnimator.AnimatorUpdateListener { animation ->
        animatedFraction = animation.animatedFraction
        postInvalidate()
    }


}



